<template>
  <div class="tools">
    <div @click="measure('drawLine')">绘制线</div>
    <div @click="measure('clearOperate')">清除痕迹</div>
    <div @click="measure('pickPoint')">点拾取</div>
    <div @click="getCamraView">获取当前相机位置</div>
    <div @click="saveCamraView">保存当前相机位置</div>

    <!-- <div @click="measure('group')">加载group模型</div> -->
  </div>
</template>
<script lang="ts" setup>
import { onMounted, ref } from 'vue';
import * as THREE from 'three';

import { distance, drawLine } from './Tools/distance';
import clearOperate from './Tools/clear';
import reduceNear from './Tools/reduceNear';
import pickPoint from './Tools/pickPoint';
import height from './Tools/height';
import returnBegin from './Tools/returnBegin';
import angle from './Tools/angle';
import group from './functions/Group';
import heighlight from './heighlight';
import clockFun from './functions/clock';
const distanceVis = ref(false);
const boxHelperVis = ref(false);
const pickPointVis = ref(false);
const heightVis = ref(false);
const angleVis = ref(false);
function measure(val: any) {
  switch (val) {
    case 'drawLine':
      distanceVis.value = !distanceVis.value;
      distance(distanceVis.value);
      break;
      break;
    case 'pickPoint':
      pickPointVis.value = !pickPointVis.value;
      pickPoint(pickPointVis.value);
      break;
    case 'clearOperate':
      clearOperate();
      break;
  }
}
function getCamraView() {
  // const pos = window.camera.position;
  // window.camera.position.set(pos.x, pos.y, pos.z);
  // // camera.lookAt(0, 0, -500);
  // window.camera.lookAt(0, 0, 90);
  // // alert(window.camera.position);
  restoreCamraView();
  console.log(window.camera.position);
  console.log(window.controls.position0);
}
function saveCamraView() {
  // 获取相机位置
  const pos = window.camera.position;

  // 获取相机朝向的目标位置
  const target = window.camera.getWorldDirection(new THREE.Vector3());

  // 将相机的位置和朝向保存到本地存储
  const cameraData = {
    position: {
      x: pos.x,
      y: pos.y,
      z: pos.z,
    },
    target: {
      x: target.x,
      y: target.y,
      z: target.z,
    },
  };

  // 将数据保存到 localStorage（作为字符串）
  localStorage.setItem('cameraView', JSON.stringify(cameraData));

  // 打印相机位置和目标
  console.log('Camera Position:', cameraData.position);
  console.log('Camera Target:', cameraData.target);
}
function restoreCamraView() {
  // 从本地存储获取相机视角数据
  const savedData = localStorage.getItem('cameraView');

  if (savedData) {
    const cameraData = JSON.parse(savedData);

    // 恢复相机位置
    window.camera.position.set(cameraData.position.x, cameraData.position.y, cameraData.position.z);

    // 恢复相机朝向目标（你可以根据目标方向恢复，或根据其他方式指定目标）
    window.camera.lookAt(cameraData.target.x, cameraData.target.y, cameraData.target.z);

    // 更新控制器（如果你使用 OrbitControls）
    if (window.controls) {
      window.controls.target.set(cameraData.target.x, cameraData.target.y, cameraData.target.z);
      window.controls.update();
    }

    console.log('Camera restored:', cameraData);
  } else {
    console.warn('No saved camera view found in localStorage.');
  }
}

onMounted(() => {});
</script>
<style scoped lang="scss">
.tools {
  position: absolute;
  left: 0px;
  top: 10px;
  width: 100px;
  height: 95vh;
  // border: 1px solid orange;
  display: flex;
  flex-direction: column;
  justify-content: space-around;
  align-items: center;

  div {
    width: 80%;
    background-color: orange;
    text-align: center;
    cursor: pointer;
  }
}
</style>
